import {
  request
} from '../api/index'

const STATE = {
  ballMillingId: null,
  dryId: null,
  dustingId: null,
  finishProductId: null,
  firingId: null,
  formingId: null,
  glazeId: null,
  glazeIdTwo: null,
  printingMethodId: null
  //   status: null  //状态（-1未完成，1是完成，2是作废）
}
/**
 * @key: 各个流程的状态
 */
const validState = (obj, key = 'status', resolve) => {
  if (key !== 'status' && key !== 'all') {
    resolve(Boolean(obj[key]))
  } else if (key === 'all') {
    const target = Object.assign({}, STATE);
    delete target.finishProductId;
    const result = Object.keys(target).every(it => Boolean(obj[it]));
    if (!result) {
      wx.showModal({
        title: '提示',
        content: '存在未保存的环节',
        success(res) {
          resolve(result);
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
    } else {
      resolve(result)
    }
  } else {
    if (obj.status === 1) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '此纪录是已完成状态，不能再修改',
        success(res) {
          resolve(false);
        }
      })
    } else if (obj.status === 2) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '此纪录已过期作废',
        success(res) {
          resolve(false);
        }
      })
    } else {
      resolve(true)
    }
  }
}

const isDone = async (id, key, data) => {
  return new Promise((resolve, reject) => {
    if (data) {
      validState(data, key, resolve);
      return
    }
    request.collectionRecordItem(id, res => {
      if (res) validState(res, key, resolve)
      else reject(false)
    })
  })
}
const EMBRY_POWDER_SPRAYING = {
  ballGrindingFineness: { // 球磨细度
    min: 0,
    max: 10,
    unit: '克/100毫升',
    desc: '0-10',
    single: true
  },
  mudFlowRate: {
    min: 10,
    max: 180,
    unit: '秒',
    desc: '10-180'
  },
  mudSpecificGravity: {
    min: 1.5,
    max: 2.0,
    desc: '1.5-2.0',
    single: true,
    unit: '克/毫升'
  }
}
const DUSTING = {
  powderBulkDensity: {
    min: 50,
    max: 200,
    desc: '50-200',
    unit: '克/100立方厘米',
    single: true
  },
  powderMoisture: {
    min: 5,
    max: 10,
    desc: '5.0-10.0',
    unit: '%'
  },
  shrinkage: {
    min: 0,
    max: 15,
    desc: '0.00-15.00',
    unit: '%',
    single: true
  },
  waterAbsorption: {
    min: 0,
    max: 15,
    desc: '0.00-15.00',
    unit: '%'
  },
  whiteness: {
    min: 15,
    max: 80,
    desc: '15.0-80.0',
    unit: '度'
  },
  lossOnIgnition: {
    min: 0,
    max: 30,
    desc: '0.00-30.00',
    unit: '%',
    single: true
  }
}
const MOLDING = {
  formingPressure: {
    min: 0,
    max: 0,
    pass: true,
    desc: '',
    unit: '无'
  },
  formingSpeed: {
    min: 2,
    max: 15,
    desc: '2.0-15.0',
    unit: '次/分钟',
    single: true
  },
  powderMoisture: {
    min: 5,
    max: 10,
    desc: '5.0-10.0',
    unit: '%'
  },
  pressSpecifications: {
    pass: true,
    min: 5,
    max: 10,
    desc: '',
    unit: '无'
  },
  specifications: {
    pass: true,
    min: [100, 2000],
    max: [100, 4000],
    desc: '100*2000-100*4000',
    unit: '毫米'
  }
}
const DRY = {
  dryingTemperature: {
    min: 150,
    max: 350,
    desc: '150-350',
    unit: '度',
    single: true
  },
  dryingSpeed: {
    min: 20,
    max: 200,
    desc: '20-200',
    unit: '分钟',
    single: true
  },
  dryStrength: {
    min: 0.5,
    max: 3.0,
    desc: '0.5-3.0',
    unit: '兆帕（Mpa)',
    single: true
  },
  dryMoisture: {
    min: 0,
    max: 3,
    desc: '0.00-3.00',
    unit: '%'
  }
}
const GLAZE = {
  glazeFlowRate: {
    min: 10,
    max: 100,
    desc: '10-100',
    unit: '秒'
  },
  glazeRatio: {
    min: 1.1,
    max: 2,
    desc: '1.1-2.0',
    unit: '克/毫升'
  },
  glazingAmount: {
    min: 0,
    max: 1500,
    desc: '0.00-1500',
    unit: '克/平方米',
    single: true
  },
  screenMesh: {
    min: 40,
    max: 325,
    desc: '40-325',
    unit: '目',
    single: true
  },
  ballGrindingFineness: {
    min: 0,
    max: 10,
    desc: '0.00-10.00',
    unit: '克/100毫升',
    single: true
  }
}

const FIRING = {
  firingCurve: {
    pass: true,
    desc: '',
    unit: '无'
  },
  firingTime: {
    min: 15,
    max: 300,
    desc: '15-300',
    unit: '分钟',
    single: true
  },
  furnaceLength: {
    min: 80,
    max: 600,
    desc: '80-600',
    unit: '米',
    single: true
  },
  measuredTemperature: {
    min: 900,
    max: 1250,
    desc: '900-1250',
    unit: '度',
    single: true
  }
}
const hasErr = (obj) => {
  let result = false;
  for (let x in obj) {
    if (obj[x]['err']) {
      result = true;
      break;
    }
  }
  return result
}
/**
 * 判断一个数组的首位是否大于限定值和末尾小于限定值
 * @param {String} value  
 * @param {Object} target 
 * @param {String} key 
 * @returns {Boolean}
 */
const inputValidHandle = (value = '', target, key) => {
  let result = true;
  if ((value && value.startsWith('-') || value.endsWith('-')) &&  target[key].pass !== true) {
    return false
  }
  if (key === 'specifications') {
    const arr = value.split('-'),
      {
        length
      } = arr;
    // 规格数据填写规范限制 300*300-1800*4000
    if (arr[0].search(/\*/g) < 0 || arr[length - 1].search(/\*/g) < 0) {
      return false;
    }
    
    const nums1_ = arr[0].split('*'),
      nums2_ = arr[arr.length - 1].split('*');
     
    if (nums1_[0] >= 1 && String(nums1_[0]).startsWith('0') || nums1_[nums1_.length - 1] >= 1 && String(nums1_[nums1_.length - 1]).startsWith('0') || nums2_[0] >= 1 && String(nums2_[0]).startsWith('0') || nums2_[nums2_.length - 1] >= 1 && String(nums2_[nums2_.length - 1]).startsWith('0')) {
      return false
    }
    try {
      const nums1 = arr[0].split('*').map(it => Number(it)),
      nums2 = arr[arr.length - 1].split('*').map(it => Number(it));
      result = nums1[0] >= target[key]['min'][0] && nums1[nums1.length - 1] >= nums1[0] && nums1[0] <= target[key]['min'][1] && nums1[nums1.length - 1] >= target[key]['max'][0] && nums1[nums1.length - 1] <= target[key]['max'][1] && nums2[0] >= target[key]['min'][0] && nums2[nums2.length - 1] >= nums2[0] && nums2[0] <= target[key]['min'][1] && nums2[nums2.length - 1] >= target[key]['max'][0] && nums2[nums2.length - 1] <= target[key]['max'][1]
    } catch (error) {
      result = false;
    }
  } else if (target[key]['single']) {

    // 如果无法实现字符类型转换
    const _v = Number(value);
    if (_v >= 1 && String(value).startsWith('0')) {
      return false
    }
    if (isNaN(_v)) {
      return false
    }
    result = target[key]['min'] <= _v && _v <= target[key]['max'];
  } else {
    const arr = value.split('-'),
      {
        length
      } = arr;
    if (length > 2) {
      return false
    }
    if (arr[0] >= 1 && arr[0].startsWith('0') || arr[length - 1] >= 1 && arr[length - 1].startsWith('0')) {
      return false
    }
    try {
      const newArr = arr.map(it => Number(it));

      result = target[key]['pass'] || newArr[0] >= target[key]['min'] && newArr[length - 1] >= newArr[0] && newArr[length - 1] <= target[key]['max'];

    } catch (error) {
      result = false;
    }
  }

  return result;
}
export {
  isDone,
  inputValidHandle,
  hasErr,
  EMBRY_POWDER_SPRAYING,
  DUSTING,
  MOLDING,
  DRY,
  GLAZE,
  FIRING
}